global class OpenCTIOperation {
    // 创建呼叫记录
    WebService static String createCallHistory(String phoneNumber, String type) {
        
        if( phoneNumber.length()==12 && phoneNumber.substring(0,2) == '01' ){
        	phoneNumber = phoneNumber.substring(1);
        }
        List<Contact> conList = [select Id, AccountId, Account.Name from Contact where MobilePhone = :phoneNumber];
        if (conList.size() == 0) {
            conList = [select Id, AccountId, Account.Name from Contact where Phone = :phoneNumber];
        }

        CallHistory__c ch = new CallHistory__c(
            Name = String.valueOf(System.now()) + '呼叫记录' + ' ' + phoneNumber,
            TelephoneNumber__c = phoneNumber,
            CommunicationStartTime__c = System.now(),
            Account__c = conList.size() == 0 ? null : conList[0].AccountId,
            Contact__c = conList.size() == 0 ? null : conList[0].Id,
            Type__c = type
        );
       // System.debug(ch);
        insert ch;
       // System.debug(ch.Id);

        return ch.Id;
    }

    // 更新呼叫记录
    WebService static void updateCallHistory(String chId) {
        CallHistory__c ch = [select Id from CallHistory__c where Id = :chId];
        ch.CommunicationEndTime__c = System.now();
         //System.debug(ch);
        update ch;
    }
}